﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using WindowsFormsApplication1.WebTest.WebTestXmlNodes.New;

namespace WindowsFormsApplication1.WebTest.WebTestXmlNodes
{
    [XmlRoot("Request", Namespace = XmlNamespace)]
    public class RequestNode : GenericWebTestNode, IXmlSerializable
    {
        public override void SetNodeProperties()
        {
            SelectedImageIndex = 1;
            ImageIndex = 1;

            Text = _nodeProperties["Url"].Value;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Notification.BeginOperation("Processing Request Node", NodeOperationEventArgs.EventImportance.Detailed);

            //Read Request attributes
            ReadXmlAttributes(reader);

            //Set node properties such as Icon, Text, etc..
            SetNodeProperties();

            //Read the <Request> opening tag
            reader.MoveToContent();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.Read();
            reader.MoveToContent();

            bool exitLoop = false;
            while (!isEmptyElement && reader.NodeType != XmlNodeType.EndElement)
            {
                //Process sub tags
                switch (reader.Name)
                {
                    case "QueryStringParameters":
                        QueryStringParametersNode queryStringParameters =
                            (QueryStringParametersNode)_queryStringParametersNodeSerializer.Deserialize(reader);
                        Nodes.Add(queryStringParameters);
                        break;

                    case "DependentRequests":
                        DependentRequestsNode dependentRequests = (DependentRequestsNode)_dependentRequestsNodeSerializer.Deserialize(reader);
                        Nodes.Add(dependentRequests);
                        break;

                    case "Headers":
                        HeadersNode headers = (HeadersNode)_headersNodeSerializer.Deserialize(reader);
                        Nodes.Add(headers);
                        break;

                    case "FormPostHttpBody":
                        FormPostHttpBodyNode formPostHttpBody =
                            (FormPostHttpBodyNode)_formPostHttpBodyNodeSerializer.Deserialize(reader);
                        Nodes.Add(formPostHttpBody);
                        break;

                    case "RequestPlugins":
                        RequestPluginsNode requestPlugins =
                            (RequestPluginsNode)_requestPluginsNodeSerializer.Deserialize(reader);
                        Nodes.Add(requestPlugins);
                        break;

                    case "ExtractionRules":
                        ExtractionRulesNode extractionRules =
                            (ExtractionRulesNode)_extractionRulesNodeSerializer.Deserialize(reader);
                        Nodes.Add(extractionRules);
                        break;

                    case "CorrelationExtractionRules":
                        CorrelationExtractionRulesNode correlationExtractionRules =
                            (CorrelationExtractionRulesNode)_correlationExtractionRulesNodeSerializer.Deserialize(reader);
                        Nodes.Add(correlationExtractionRules);
                        break;

                    case "ValidationRules":
                        ValidationRulesNode validationRules =
                            (ValidationRulesNode)_validationRulesNodeSerializer.Deserialize(reader);
                        Nodes.Add(validationRules);
                        break;


                    case "StringHttpBody":
                        StringHttpBodyNode stringHttpBody =
                            (StringHttpBodyNode)_stringHttpBodyNodeSerializer.Deserialize(reader);
                        Nodes.Add(stringHttpBody);
                        break;

                    default:
                        Notification.DebugMessage("Unknown tag: " + reader.Name);
                        exitLoop = true;
                        break;
                }

                reader.MoveToContent();

                if (exitLoop) break;
            }


            if (!isEmptyElement)
                reader.ReadEndElement();

            Notification.EndOperation();
        }


        #endregion


    }
}
